package org.wicket.jbpm.task.panel;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.lang.Bytes;
import org.wicket.jbpm.engine.spring.JbpmProcessEngine;
import org.wicket.jbpm.page.admin.JbpmProcessDefPage;

/**
 * 
 * @author Emmanuel Nollase - emanux
 * created 2009 10 7 - 16:20:45
 */
public class UploadProcessPanel extends Panel
{

    @SpringBean private JbpmProcessEngine jbpmProcessEngine;
    
    public UploadProcessPanel(String id)
    {
	super(id);
	
	final Form processForm = new JbpmUploadForm("jbpmProcess");
	add(processForm);
	
    }

    private class JbpmUploadForm extends Form 
    {

	private FileUploadField fileUploadField;
	
	public JbpmUploadForm(String id)
	{
	    super(id);
	   setMultiPart(true);
	   add(fileUploadField = new FileUploadField("fileInput"));
	   setMaxSize(Bytes.megabytes(10));
	}
	
	 @Override
	 protected void onSubmit()
	 {
	     final FileUpload upload = fileUploadField.getFileUpload();
	     
	     try
	    {
		if(upload != null)
		 {
		    jbpmProcessEngine.deployDefinition(upload.getInputStream(), upload.getContentType());
		    
		    setRedirect(true);
		    setResponsePage(JbpmProcessDefPage.class);
		 }
	    } catch (Exception e)
	    {
		e.printStackTrace();
		error("Error uploading process definition");
	    }
	     
	 }
	
    }
}

